/*
 * This file is part of ksPlatform
 * 
 * Copyright (c) 2014-2015
 * 
 * This file is dual-licensed under the AGPLv3 (http://www.gnu.org/licenses/agpl-3.0.html) 
 * 
 * If you are unsure which license is appropriate for your use, please contact the sales department
 * at 97041437@qq.com.
 
package cn.com.ksplatform.core.loader;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import org.xvolks.jnative.JNative;
import org.xvolks.jnative.Type;
import org.xvolks.jnative.exceptions.NativeException;
import org.xvolks.jnative.pointers.Pointer;
import org.xvolks.jnative.pointers.memory.MemoryBlockFactory;

import cn.com.ksplatform.common.exception.FileHeaderException;

public class GJar {
	
	private static byte[] fileHeader; 
	
	static{
		JNative n = null;
		Pointer pointer = null;
        try {              
        	n = new JNative("platform.dll", "pbyte");
            pointer = new Pointer(MemoryBlockFactory.createMemoryBlock(10));
            n.setParameter(0, pointer);
            n.invoke();
            fileHeader = pointer.getMemory();
        } catch (Exception e) {
			e.printStackTrace();
			throw new FileHeaderException("加密包头文件加载异常");
		}finally{
			try {
				n=null;
				pointer.dispose();
			} catch (NativeException e) {
				e.printStackTrace();
			}
		}
	}
	
	private static int cvt(int len,int step){
    	try {
    		JNative n = new JNative("platform.dll", "convert");
			n.setParameter(0, len);
			n.setParameter(1, step);
			n.setRetVal(Type.INT);
	    	n.invoke();
	    	return n.getRetValAsInt();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (NativeException e) {
			e.printStackTrace();
		}
    	return -1;
    }
	
	public static void main(String[] args) throws IOException {
		String injar = "e:\\jar\\t.jar";
		String outjar = "e:\\jar\\t_out.jar";
		FileInputStream ins = new FileInputStream(injar);
		FileOutputStream out = new FileOutputStream(outjar);
		int content = 0;
		int step = 0;
		out.write(fileHeader);
		
		while((content = ins.read()) != -1){
			if(step % 512 == 0){
				out.write(cvt(content,step));
    		}else{
    			out.write(content);
    		}
			step++;
		}
		out.flush();
		out.close();
		ins.close();
		
	}
}
*/